package com.huluobo.a2204parkdemo.park

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.DatePicker
import androidx.core.text.set
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2204parkdemo.R
import com.huluobo.a2204parkdemo.adapter.CarImgAdapter
import com.huluobo.a2204parkdemo.adapter.ColorAdapter
import com.huluobo.a2204parkdemo.databinding.ActivityParkingValueBinding
import com.huluobo.base.bean.ColorBean
import com.huluobo.base.bean.ColorBeanItem
import com.huluobo.base.bean.ParkingBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.Calendar

@AndroidEntryPoint
class ParkingValueActivity : BaseActivity<ActivityParkingValueBinding, ParkViewModel>() {
    private lateinit var parkingBeanItem: ParkingBeanItem
    private lateinit var colorAdapter: ColorAdapter
    private lateinit var carImgAdapter: CarImgAdapter
    override fun initData() {
        parkingBeanItem = intent.getSerializableExtra(Const.INTENT_PARK_BEAN) as ParkingBeanItem
        Log.i("TagA", "value:$parkingBeanItem")
        viewModel.getColor()

        viewModel.color.observe(this) {
            val list = Gson().fromJson(it, ColorBean::class.java)
            colorAdapter.data.clear()
            colorAdapter.data.addAll(list)
            colorAdapter.notifyDataSetChanged()
        }

        viewModel.addParkingSuccess.observe(this) {
            ToastUtils.showLong("添加申请成功")
            finish()
        }
    }

    override fun initView() {
        binding.parkValueHeader.headerTitle.text = "车位申请"
        binding.parkValueHeader.headerBack.setOnClickListener { finish() }

        binding.parkCarColorRv.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        colorAdapter = ColorAdapter()
        binding.parkCarColorRv.adapter = colorAdapter

        binding.parkCarPicRv.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false);
        carImgAdapter = CarImgAdapter()
        binding.parkCarPicRv.adapter = carImgAdapter

        carImgAdapter.data.add("+")
        carImgAdapter.notifyDataSetChanged()

        carImgAdapter.setOnItemClickListener { adapter, view, position ->
            if (position == adapter.data.size - 1) {
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }
        }

        colorAdapter.setOnItemClickListener { adapter, view, position ->
            binding.parkCarColorSelectIv.setBackgroundColor(Color.parseColor(colorAdapter.data[position].colorValue))
        }

        binding.parkDateNowTv.text = getTime()
        binding.parkUsernameTv.text = SPUtils.getInstance().getString(Const.PARAM_USERNAME)
        binding.parkCarNumEt.hint = "津AF75776"

        binding.parkStartTimeIv.setOnClickListener {
            showDateDialog(binding.parkStartTimeNowTv)
        }

        binding.parkEndTimeIv.setOnClickListener {
            showDateDialog(binding.parkEndTimeNowTv)
        }

        binding.parkCommitBtn.setOnClickListener {
            if (binding.parkStartTimeNowTv.text.isNotEmpty() && binding.parkEndTimeNowTv.text.isNotEmpty()) {
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_PARK_ID] = 0
                map[Const.PARAM_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_USER_ID)
                map[Const.PARAM_PARK_NAME] = SPUtils.getInstance().getString(Const.PARAM_USERNAME)
                map[Const.PARAM_START_TIME] = binding.parkStartTimeNowTv.text
                map[Const.PARAM_END_TIME] = binding.parkEndTimeNowTv.text
                map[Const.PARAM_PARK_TIME] = getTime()

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                viewModel.addParking(body)
            } else {
                ToastUtils.showLong("请选择开始和结束时间")
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data?.extras//拿到相册返回的bundle
            val bean = bundle?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)//再通过图片拾取器提供key拿到对应的bean
            val filePath = bean?.path
            if (filePath != null) {
                carImgAdapter.data.add(0, filePath)
                carImgAdapter.notifyDataSetChanged()
            }
        }
    }
}